package cn.com.darly.videoandroidpm.ui.presenter;

import com.darly.common.DLog;
import com.darly.common.ToastApp;
import com.darly.common.retrofit.FrameListener;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.MessageInfo;
import cn.com.darly.videoandroidpm.common.DateUtils;
import cn.com.darly.videoandroidpm.common.dao.ConversationMessageInfo;
import cn.com.darly.videoandroidpm.common.sugar.SugarRecord;
import cn.com.darly.videoandroidpm.ui.biz.ConversationListener;
import cn.com.darly.videoandroidpm.widget.shapeloading.ShapeLoadingDialog;


/**
 * @author zhangyh2 s 上午10:57:39 TODO 控制器 页面动作操作
 */
public class ConversationPresenter {

    private  ConversationListener userBiz;
    private  ConversationListener. ConversationInterface main;

    private ShapeLoadingDialog loading;



    public ConversationPresenter( ConversationListener. ConversationInterface main) {
        this.main = main;
        this.userBiz = new  ConversationListener();
        loading = new ShapeLoadingDialog.Builder((BaseActivity)main)
                .loadText("和服务端同步消息记录...")
                .build();
    }

    public void saveConversationMessage(String userid, String content, String friendid){
        final String  key = UUID.randomUUID().toString();
        DLog.i(key);
        ConversationMessageInfo sendMsg = new ConversationMessageInfo();
        sendMsg.setMsgFrom(userid);
        sendMsg.setMsgTo(friendid);
        sendMsg.setMsgOther("");
        sendMsg.setMsgContent(content);
        sendMsg.setMsgTime(DateUtils.now().toString());
        sendMsg.setMsgType(ConversationMessageInfo.TYPE_PHONE+"");
        sendMsg.setMsgSend(false);
        sendMsg.setMsgKey(key);
        sendMsg.save();
        main.addNewMessage(sendMsg);

        //上傳消息
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("friendid", friendid);
        params.put("type", ConversationMessageInfo.TYPE_PHONE+"");
        params.put("content", content);
        main.setDisableClick();
        userBiz.onsendMsg(params, new FrameListener<MessageInfo>() {
            @Override
            public void onSucces(MessageInfo info) {
                main.setEnableClick();
                List<ConversationMessageInfo> dbinfo = SugarRecord.find(ConversationMessageInfo.class,"msg_key = ?",key);
                if (dbinfo!=null&&dbinfo.size() == 1){
                    ConversationMessageInfo savedInfo = dbinfo.get(0);
                    savedInfo.setMsgId(info.getId());
                    savedInfo.setMsgFrom(info.getMsgFrom());
                    savedInfo.setMsgTo(info.getMsgTo());
                    savedInfo.setMsgType(info.getMsgType());
                    savedInfo.setMsgTime(info.getMsgTime());
                    savedInfo.setMsgContent(info.getMsgContent());
                    savedInfo.setMsgOther(info.getMsgOther());
                    savedInfo.setMsgSend(true);
                    savedInfo.save();
                    main.sendMessageOK(savedInfo);
                    DLog.i(key);
                }else {
                    ToastApp.showToast("消息逻辑错误，请重试！");
                }

            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onConversationFailed(info);
                }
            }
        });

    }

    public void getMessageFromDB(String userid,String friendid) {

        final List<ConversationMessageInfo> db = SugarRecord.find(ConversationMessageInfo.class,"(msg_from = ? and msg_to = ?) or (msg_from = ? and msg_to = ?)",userid,friendid,friendid,userid);
            //網絡請求歷史信息
            Map<String, String> params = new LinkedHashMap<String, String>();
            params.put("friendid", friendid);
            params.put("messageinfoNub",db == null?"0":db.size()+"");
            userBiz.onStart(loading);
            userBiz.onfindMsg(params, new FrameListener<List<ConversationMessageInfo>>() {
                @Override
                public void onSucces(List<ConversationMessageInfo> data) {
                    main.setEnableClick();
                    userBiz.onStop(loading);
                    if (db == null) {
                        main.onConversationSuccess(data);
                    }else {
                        db.addAll(data);
                        main.onConversationSuccess(db);
                    }
                }

                @Override
                public void onFaild(int code, String info) {
                    main.setEnableClick();
                    userBiz.onStop(loading);
                    if (db != null) {
                        main.onConversationSuccess(db);
                    }
                }
            });
    }
}